Page({
  data: {
    schema:[
    ],
    formData:{
    },
    title: '',
    result: '',
    remark: '',
    apiName: '',
    downloadProgress: 0,
    isDownloaded: false,
    workerResult: ''
  },
 // 预下载包含Worker的分包
 preDownloadWorkerSubpackage(e) {
  const that = this;
        // 开始下载分包
        const downloadTask = wx.preDownloadSubpackage({
          packageType: "workers",
          success(res) {
            console.log('Worker分包下载成功', res);
            that.setData({
              isDownloaded: true,
              result: res,
              apiName: e.currentTarget.dataset.apiname
            });
          },
          fail(err) {
            console.error('Worker分包下载失败', err);
            that.setData({
              result: err,
              apiName: e.currentTarget.dataset.apiname
            });
            wx.showToast({
              title: '分包下载失败',
              icon: 'none'
            });
          },
          complete() {
            console.log('Worker分包下载操作完成');
          },
        })
        // 监听下载进度
downloadTask.onProgressUpdate(res => {
  console.log('下载进度：', res.progress)
  console.log('已下载字节：', res.totalBytesWritten)
  console.log('总字节数：', res.totalBytesExpectedToWrite)
  
  // 可以在这里更新UI显示进度，例如：
  that.setData({
    downloadProgress: res.progress
  })
})
that.task = downloadTask
},
onProgressUpdate(e){
  const that = this
  if(that.task){
    console.log(that.task)
  that.task.onProgressUpdate(res => {
    console.log('下载进度', res.progress);
    that.setData({
      downloadProgress: res.progress,
      result: res,
      apiName: e.currentTarget.dataset.apiname
    });
  })
}else{
  that.setData({
    result: "未发现分包",
    apiName: e.currentTarget.dataset.apiname
  });
}
},
// 使用分包中的Worker
useWorker(e) {
  const that = this
  if (!that.data.isDownloaded) {
    wx.showToast({
      title: '分包未下载完成',
      icon: 'none'
    });
    return;
  }
  const worker = wx.createWorker('/workers/fib/index.js')
  worker.terminate(); // 终止Worker
  console.log(worker)
  that.setData({
    result: "worker:"+worker,
    apiName: e.currentTarget.dataset.apiname
  });
},
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: options.title,
    })
    
    this.setData({
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
  }
})
